<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class PostController extends Controller
{
   //前台帖子列表
	public function getIndex(Request $request)
	{
		//获取帖子列表
		if (!empty($request->input('sid'))) {
         $p = DB::table('post')->select(DB::raw('post.pname,post.pid,block.sname,post.bbid,users.username,post.ctr,block.bname,count(followup.fid) as num'))->leftJoin('block','post.bbid','=','block.bid')->leftJoin('users','post.puid','=','users.uid')->leftJoin('followup','post.pid','=','followup.fpid')->where('sid','=',$request->input('sid'))->groupBy('post.pid')->get();
			
		}else{

         $p = DB::table('post')->select(DB::raw('post.pname,post.pid,post.bbid,users.username,post.ctr,block.bname,count(followup.fid) as num'))->leftJoin('block','post.bbid','=','block.bid')->leftJoin('users','post.puid','=','users.uid')->leftJoin('followup','post.pid','=','followup.fpid')->groupBy('post.pid')->get();
		}
		$user=DB::table('users')->where('uid',session('uid'))->first();

         $block	= DB::table('block')->get(); 
		//解析模版
		return view('Home.post.index',['post'=>$p,'block'=>$block,'user'=>$user]);
	}

	//帖子详情
	public function getInfo(Request $request)
	{
		$pid = $request->input('pid');
		
		//差询帖子详情
        $post = DB::table('post')->select(DB::raw('*,count(followup.fid) as num'))->leftJoin('block','post.bbid','=','block.bid')->leftJoin('users','post.puid','=','users.uid')->leftJoin('followup','post.pid','=','followup.fpid')->where('post.pid','=',$pid)->groupBy('followup.fpid')->get()[0];
        
        // dd($post);
        //查询回帖列表
        $fuzan = DB::table('followup')->where('fpid',$pid)->where('lnum','>','100')->leftJoin('users','followup.fuid','=','users.uid')->orderBy('lnum','desc')->skip(0)->take(3)->get();
        $fu = DB::table('followup')->where('fpid',$pid)->leftJoin('users','followup.fuid','=','users.uid')->paginate(5);
        // dd($fu);
		$user=DB::table('users')->where('uid',session('uid'))->first();

        $list = $request->all();
        //解析模版
        return view('home.post.postinfo',['post'=>$post,'fuzan'=>$fuzan,'fu'=>$fu,'list'=>$list,'user'=>$user]);
	}

	
	//帖子回复
	public function postFollowup(Request $request)
	{
		// dd($request->all());
		//提取数据
		$data = $request->except('_token');
		//获取当前时间
		$data['fctime'] = date('Y-m-d H:i:s',time());
		//浏览器类型
		$data['browser'] = $_SERVER['HTTP_USER_AGENT']; 
		$data['fuid'] = session('uid');
		// $data['fuid'] = 7;
		//存入数据库
		$res = DB::table('followup')->insert($data);
		if ($res) {
			//回复成功 修改帖子相应内容
			$res = DB::update('update post set fuid = '.$data['fuid'].' , ftime = "'.$data['fctime'].'" where pid = ?', [$data['fpid']]);

			//跳转页面
			 return redirect('/home/post/info?pid='.$data['fpid'])->with('success','回复成功');
		}else{
			//回复失败
			 return back()->with('error','回复失败'); 
		}
		// dd($data);
	}

	//点赞添加
	public function getAddzan(Request $request)
	{
		//执行加一
		$res = DB::update('update followup set lnum = lnum + 1 where fid = ?',[$request->input('pid')]);
		if ($res) {
			//修改成功
			echo 1;
		}else{
			//修改失败
			echo 0;
		}
	}

	//阅览量添加
	public function getAddyue(Request $request)
	{
		$pid = $request->input('pid');
		//贴子阅览量加1
		$res = DB::update('update post set ctr = ctr + 1 where pid = ?', [$pid]);
		if ($res) {
			//修改成功
			echo 1;
		}else{
			//修改失败
			echo 0;
		}
	}

	//发帖
	public function getAdd(Request $request)
	{
		//提取bid
		$bid = $request->input('bid');
		//查询相应版块名
		$block = DB::table('block')->select(DB::raw('bname,sname,bid'))->where('bid',$bid)->get()[0];
		$user=DB::table('users')->where('uid',session('uid'))->first();
		
		// dd($block);
		return view('home.post.add',['block'=>$block,'user'=>$user]);
	}

	//发帖数据处理
	public function postInsert(Request $request)
	{
		// dd($request->all());
		$data = $request->only('pname','pcont','bbid');
		$data['pctime'] = date('Y-m-d H:i:s',time());
		$data['browser'] = $_SERVER['HTTP_USER_AGENT']; 
		$data['puid'] = session('uid');
		// $data['puid'] = 7;
		//插入数据
		$res = DB::table('post')->insertGetId($data);
		// dd($data);
		//判断插入结果
		if ($res) {
            //执行成功
            return redirect('/home/block/index?bid='.$data['bbid'])->with('success','插入成功');
        }else{
            //执行失败
            return back()->with('error','插入失败'); 
        }
	}

	//收藏帖子
	public function getShoucang(Request $request)
	{
		$data = $request->only('pid');
		$data['uid'] = session('uid');
		$ss = DB::select('select pid from person where uid = ?',[$data['uid']]);
		foreach ($ss as $k => $v) {
			if ($v->pid == $data['pid']) {
				echo 2;
				die;
			}
		}
		// dd($ss); 
		$res = DB::table('person')->insert($data);
		if ($res) {
			//收藏成功
			echo 1;
		}else{
			//收藏失败
			echo 0;
		}
	}
}
